package com.datang.pet.service.impl;/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-06-12 08:43
 * @notify
 * @version 1.0
 */

import com.datang.pet.common.utils.MD5;
import com.datang.pet.common.utils.StringUtils;
import com.datang.pet.mapper.LabelMapper;
import com.datang.pet.mapperExtend.LabelMapperExtend;
import com.datang.pet.pojo.Label;
import com.datang.pet.service.LabelService;
import com.datang.pet.vo.GetLabelListResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class LabelServiceImpl implements LabelService {
    @Autowired
    private LabelMapper labelMapper;

    @Autowired
    private LabelMapperExtend labelMapperExtend;


    /*
     * @author 顶风少年
     * @date 2019/6/12 9:34
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param label
     * @result void
     * @notify 添加标签
     * version 1.0
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addLabel(Label label) throws Exception {

        //md5解密userId(普通用户)
        try {
            label.setUserId(MD5.decryptData(label.getUserId()));
        } catch (Exception e) {
            throw new Exception("userId解密出错！");
        }

        try {
            label.setId(StringUtils.getId());
            label.setDate(new Date());
            labelMapper.insert(label);
        } catch (Exception e) {
            throw new Exception("新建标签失败");
        }

    }

    /*
     * @author 顶风少年
     * @date 2019/6/8 18:21
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @result java.util.List<com.datang.pet.vo.LabelRepositoryResponse>
     * @notify 获取热门标签
     * version 1.0
     */
    @Override
    public List<GetLabelListResponse> getLabelList() throws Exception {

        List<GetLabelListResponse> labelRepositories = new ArrayList<>();
        try {
            //调用查询接口
            labelRepositories = labelMapperExtend.getLabelList();
            return labelRepositories;
        } catch (Exception e) {
            throw new Exception("获取标签失败");
        }
    }
}
